import 'package:flutter/material.dart';

import '../page/animal_detail_page/animal_detail_page.dart';
import '../page/plant_detail_page/plant_detail_page.dart';
import '../util/constant/constants.dart';
import 'image_with_title.dart';

Widget customGridView(
    {required List itemList, required bool isPlant,}) {
  return SliverGrid.builder(
    gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
      crossAxisCount: 2,
      crossAxisSpacing: 10,
      mainAxisSpacing: 10,
      mainAxisExtent: 280,
    ),
    itemCount: itemList.length,
    itemBuilder: (context, index) {
      return GestureDetector(
        onTap: () {
          //跳转到详情页
          if (isPlant == false) {
            Navigator.of(context).push(
              MaterialPageRoute(
                builder:
                    (_) =>
                    AnimalDetailPage(
                      itemList[index].id,
                    ),
              ),
            );
          } else {
            Navigator.of(context).push(
              MaterialPageRoute(
                builder:
                    (_) =>
                    PlantDetailPage(
                      itemList[index].id,
                    ),
              ),
            );
          }
        },
        child: ImageWithTitle(
          title: itemList[index].name,
          location: itemList[index].region,
          imageUrl: imageUrlPrefix + itemList[index].image,
        ),
      );
    },
  );
}